package com.imay.loader;

import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.imay.live.R;
import com.imay.live.view.custom.DataLoadStatusView;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.List;

/**
 * Created by jeaman on 2017/9/26.
 * 通用RecyclerView数据加载器
 */

public class DataLoader implements BaseQuickAdapter.RequestLoadMoreListener,View.OnClickListener {
    protected TwinklingRefreshLayout mRefreshLayout;
    protected RecyclerView mRecyclerView;
    private BaseQuickAdapter mAdapter;
    private DataLoadStatusView mLoadStatusView;

    private OnRefreshListener mListener;
    private boolean mEnableRefresh; //是否开启下拉刷新
    private boolean mEnableLoadMore;    //是否开启下拉加载更多
    private boolean mLoadMoreEnable;    //下拉刷新时记录当前是否开启状态

    public DataLoader(OnRefreshListener listener) {
        mListener = listener;
    }

    public void initRecyclerView(RecyclerView recyclerView, RecyclerView.LayoutManager layoutManager, BaseQuickAdapter adapter, boolean enableLoadMore) {
        mRecyclerView = recyclerView;
        mAdapter = adapter;
        mEnableLoadMore = enableLoadMore;

        if (mEnableLoadMore) {
            mAdapter.setOnLoadMoreListener(this, mRecyclerView);    //使用adapter的加载更多
        }

        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setHasFixedSize(true);

        mLoadStatusView = new DataLoadStatusView(mRecyclerView.getContext());
        mAdapter.setEmptyView(mLoadStatusView);
    }

    public void initRefreshLayout(TwinklingRefreshLayout refreshLayout) {
        if (refreshLayout != null) {
            mEnableRefresh = true;
            mRefreshLayout = refreshLayout;
            mRefreshLayout.setEnableRefresh(false); //先关闭下拉刷新，数据加载后再开启
            mRefreshLayout.setEnableLoadmore(false);    //不使用该控件的加载更多
            mRefreshLayout.setEnableOverScroll(false);
            mRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
                @Override
                public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                    onRefreshRequested();
                }
            });
        }
    }

    //第一次请求或者下拉刷新数据
    public void setNewData(@Nullable List data) {
        mAdapter.setNewData(data);
        if (mEnableRefresh) {    //开启下拉刷新
            enableRefresh();
        }
        if (data != null && !data.isEmpty()) {
            if (mEnableLoadMore) {
                mAdapter.disableLoadMoreIfNotFullPage();
            }
        } else {
            //没有数据时，显示空白页
            showEmptyView();
        }
    }

    //加载更多数据
    public void addData(@Nullable List data) {
        if (data != null && !data.isEmpty()) {
            mAdapter.addData(data);
        } else {    //没有数据时，加载结束
            loadMoreEnd();
        }
    }

    public int getDataSize() {
        return mAdapter.getData().size();
    }

    private void showLoadingView() {
        mLoadStatusView.showLoading();
        mLoadStatusView.setOnClickListener(null);
    }

    //显示空白页面
    private void showEmptyView() {
        mLoadStatusView.showEmpty();
        mLoadStatusView.setOnClickListener(null);
    }

    //显示错误页面
    public void showErrorView() {
        mLoadStatusView.showError();
        mLoadStatusView.setOnClickListener(this);
    }

    //刷新完成
    public void refreshComplete() {
        if (mLoadMoreEnable) {  //恢复加载更多
            mAdapter.setEnableLoadMore(true);
        }
        if (mRefreshLayout != null) {
            mRefreshLayout.finishRefreshing();
        }
    }

    //加载完成
    public void loadMoreComplete() {
        mAdapter.loadMoreComplete();
    }

    //加载失败
    public void loadMoreFail() {
        mAdapter.loadMoreFail();
    }

    //加载结束
    private void loadMoreEnd() {
        mAdapter.loadMoreEnd(true);
    }

    private void enableRefresh() {
        if (mRefreshLayout != null) {
            mRefreshLayout.setEnableRefresh(true);
        }
    }

    @Override
    public final void onLoadMoreRequested() {
        if (mListener != null) {
            mListener.onLoadMoreRequested();
        }
    }

    private void onRefreshRequested() {
        if (mEnableLoadMore && mAdapter.isLoadMoreEnable()) {//下拉刷新时，禁止加载更多
            mLoadMoreEnable = mAdapter.isLoadMoreEnable();
            mAdapter.setEnableLoadMore(false);
        }
        if (mListener != null) {
            mListener.onRefreshRequested();
        }
    }

    @Override
    public void onClick(View v) {
        if (v == mLoadStatusView) { //点击加载错误，重新请求数据
            if (mListener != null) {
                showLoadingView();  //显示loading
                mListener.onRetryRequested();
            }
        }
    }

    public interface OnRefreshListener {
        void onRefreshRequested();
        void onLoadMoreRequested();
        void onRetryRequested();
    }

}
